﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;

namespace HRM.UI.HRAdminUI.Notice
{
    public partial class PublishNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllNotices();
            }
        }

        private void GetAllNotices()
        {
            NoticeManager manager = new NoticeManager();
            noticesGdv.DataSource = manager.GetAllNotices();
            noticesGdv.DataBind();
        }

        protected void Published(object o, EventArgs e)
        {
            try
            {
                DAL.DAO.Notice notice = new DAL.DAO.Notice();
                notice.Date = txtDate.Value;
                notice.Subject = txtSubject.Value;
                notice.Summary = txtSummary.Value.Replace("13", "</br>");

                string noticeImage = Path.GetFileName(UploadImage.PostedFile.FileName);
                UploadImage.PostedFile.SaveAs(Server.MapPath("~/NoticeImage/") + noticeImage);
                notice.NoticeImage = "~/NoticeImage/" + noticeImage;
                notice.NoticeBy = txtAuthoruty.Value;

                NoticeManager manager = new NoticeManager();
                if (manager.PublishNotice(notice))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Notice Published Successfully";
                    ClearInputs();
                    GetAllNotices();
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Notice Not Published";
                }

            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        private void ClearInputs()
        {
            txtDate.Value = "";
            txtSubject.Value = "";
            txtSummary.Value = "";
            txtAuthoruty.Value = "";
        }

        protected void noticesGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            noticesGdv.PageIndex = e.NewPageIndex;
            GetAllNotices();
        }

        protected void noticesGdv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            noticesGdv.EditIndex = -1;
            GetAllNotices();

        }

        protected void noticesGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            try
            {
                NoticeManager noticeManager=new NoticeManager();
                DAL.DAO.Notice notice = new DAL.DAO.Notice();
                int id = Convert.ToInt32(((Label)noticesGdv.Rows[e.RowIndex].FindControl("lblID")).Text);
                if (noticeManager.DeleteNotice(id))
                {
                    msgLbl.Text = "Notice Deleted Successfully";
                    msgLbl.ForeColor = Color.Green;

                }
                else
                {
                    msgLbl.Text = "Sorry Notice not Deleted ";
                    msgLbl.ForeColor = Color.Red;
                }
               


            }
            catch (Exception ex)
            {

                msgLbl.Text = ex.Message;
                msgLbl.ForeColor = Color.Red;
            }
            

        }

       
    }
}